// 挂载<script>节点
const fs = require('fs');
const { getMatchRowNumber, insertInLineNo } = require('../scripts/utils');

const mountScript = (htmlPath) => {
  let lineNo = -1;
  try {
    lineNo = getMatchRowNumber('127.0.0.1:63221', htmlPath)[0];
  } catch (err) {
    lineNo = -1;
  }
  if (lineNo < 0) {
    // 没插入
    const bottomLineNo = getMatchRowNumber('</body>', htmlPath)[0];
    const str =
      '<div id="ROOT"></div>\\n<script src="http://127.0.0.1:63221"></script>';
    insertInLineNo(bottomLineNo, str, htmlPath);
  }
};

module.exports = mountScript;
